package archer.framework.common.protocol.result;

import archer.framework.common.entity.validate.BeanValidators;
import archer.framework.common.entity.validate.ValidateResult;
import archer.framework.common.repository.condition.QueryCondition;
import archer.framework.protocol.result.ExecuteResultProcessor;
import archer.framework.security.token.Subject;
import archer.framework.security.utils.SubjectUtils;
import archer.framework.utils.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author christ
 * @date 2016/4/26
 */
public class WebExecuteResultProcessor extends ExecuteResultProcessor {

    /**
     * 获得当前请求线程对应的HttpServletRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest() {

        return ServletUtils.getCurrentRequest();
    }

    /**
     * 获得当前请求线程对应的HttpServletResponse
     *
     * @return
     */
    public static HttpServletResponse getResponse() {

        return ServletUtils.getCurrentResponse();
    }

    /**
     * bean校验
     *
     * @param bean
     * @return
     */
    public static ValidateResult entityValidate(Object bean) {

        return BeanValidators.entityValidate(bean);
    }

    /**
     * bean校验
     *
     * @param bean
     * @return
     */
    public static ValidateResult entityValidate(List<Object> bean) {

        return BeanValidators.entityValidate(bean);
    }

    /**
     * 获得当前登录用户id
     *
     * @return
     */
    public Object getCurrentUserId() {

        return SubjectUtils.getCurrentUserId();
    }

    /**
     * 获得当前用户
     *
     * @return
     */
    public Subject getCurrentUser() {

        return SubjectUtils.getSubject();
    }

    public static QueryCondition asCondition(String condition) {

        return deserialize(condition, QueryCondition.class);
    }
}
